import { AnyZodObject, z } from 'zod';
import { zodBuildSchemeFragment, zodExt, zodRuntime } from '../shared';
import { zodPlatformBaseFragment } from '../platforms/fragments/base';

export const zodCommonSchemaFragment = z
    .object({
        includedPermissions: z
            .array(z.string())
            .describe(
                "Allows you to include specific permissions by their KEY defined in `permissions` object. Use: `['*']` to include all"
            ),
        excludedPermissions: z
            .array(z.string())
            .describe(
                "Allows you to exclude specific permissions by their KEY defined in `permissions` object. Use: `['*']` to exclude all"
            ),
        id: z.string().describe('Bundle ID of application. ie: com.example.myapp'),
        idSuffix: z.string().optional(),
        version: z.string().describe('Semver style version of your app'),
        versionCode: z.string().describe('Manual verride of generated version code'),
        versionFormat: z.string()
            .describe(`Allows you to fine-tune app version defined in package.json or renative.json.
    If you do not define versionFormat, no formatting will apply to version.
    `),
        versionCodeFormat: z.string().describe(`Allows you to fine-tune auto generated version codes.
    Version code is autogenerated from app version defined in package.json or renative.json.
    `),
        versionCodeOffset: z.number().optional(),
        title: z
            .string()
            .describe(
                'Title of your app will be used to create title of the binary. ie App title of installed app iOS/Android app or Tab title of the website'
            ),
        description: z
            .string()
            .describe(
                'General description of your app. This prop will be injected to actual projects where description field is applicable'
            ),
        author: z.string().describe('Author name'),
        license: z.string().describe('Injects license information into app'),
        includedFonts: z
            .array(z.string())
            .describe(
                'Array of fonts you want to include in specific app or scheme. Should use exact font file (without the extension) located in `./appConfigs/base/fonts` or `*` to mark all'
            ),
        backgroundColor: z
            .string()
            .min(4)
            .max(9)
            .regex(/^#/)
            .describe('Defines root view backgroundColor for all platforms in HEX format'),
        splashScreen: z.boolean().describe('Enable or disable splash screen'),
        fontSources: z
            .array(z.string())
            .describe(
                'Array of paths to location of external Fonts. you can use resolve function here example: `{{resolvePackage(react-native-vector-icons)}}/Fonts`'
            ),
        assetSources: z
            .array(z.string())
            .describe(
                'Array of paths to alternative external assets. this will take priority over ./appConfigs/base/assets folder on your local project. You can use resolve function here example: `{{resolvePackage(@flexn/template-starter)}}/appConfigs/base/assets`'
            ),
        includedPlugins: z
            .array(z.string())
            .describe(
                "Defines an array of all included plugins for specific config or buildScheme. only full keys as defined in `plugin` should be used.\n\nNOTE: includedPlugins is evaluated before excludedPlugins. Use: `['*']` to include all"
            ),
        excludedPlugins: z
            .array(z.string())
            .describe(
                "Defines an array of all excluded plugins for specific config or buildScheme. only full keys as defined in `plugin` should be used.\n\nNOTE: excludedPlugins is evaluated after includedPlugins. Use: `['*']` to exclude all"
            ),
        runtime: z.optional(zodRuntime),
        custom: z.optional(zodExt),
    })
    .partial();

export const zodCommonSchema: AnyZodObject = zodCommonSchemaFragment
    .merge(
        z
            .object({
                buildSchemes: z.optional(
                    z.record(
                        z.string(),
                        zodCommonSchemaFragment.merge(zodBuildSchemeFragment.merge(zodPlatformBaseFragment))
                    )
                ),
            })
            .partial()
    )
    .describe('Common config props used as default props for all available buildSchemes');
